package ph.com.gs3.formalistics.model.facades;

import ph.com.gs3.formalistics.model.LocalDataAccessObject.UpdateFailedException;
import ph.com.gs3.formalistics.model.ldao.CommentsLDAO;
import ph.com.gs3.formalistics.model.vo.business.Comment;
import ph.com.gs3.formalistics.model.vo.business.User;
import android.content.Context;

public class CommentsDataWriter {

	protected User user;
	protected CommentsLDAO commentsLDAO;

	public CommentsDataWriter(Context context, User user) {
		commentsLDAO = new CommentsLDAO(context);
	}

	public Comment saveOrUpdateComment(Comment comment) {

		String webId = comment.getWebId();
		int documentId = comment.getDocumentId();

		Comment savedComment = null;

		try {
			commentsLDAO.open();
			Comment existingComment = commentsLDAO.getComment(webId, documentId);

			if (existingComment == null) {
				savedComment = commentsLDAO.saveComment(comment);
			} else {
				try {
					savedComment = commentsLDAO.updateComment(webId, documentId, comment);
				} catch (UpdateFailedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} finally {
			commentsLDAO.close();
		}

		return savedComment;

	}

}
